package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper,WmSensitive> implements WmSensitiveService {

    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;

    /**
     * 新增敏感词
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult saveSensitive(WmSensitive wmSensitive) {
        //1.参数校验
        if (wmSensitive.getSensitives() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        if (sensitiveExist(wmSensitive)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //2.判断敏感词是否存在

        //3.保存
        wmSensitive.setCreatedTime(new Date());
        wmSensitiveMapper.insert(wmSensitive);

        return ResponseResult.okResult("敏感词新赠成功！");
    }

    //判断是否存在敏感词
    private boolean sensitiveExist(WmSensitive wmSensitive) {
        //2.1查询所有的敏感词信息
        List<WmSensitive> wmSensitives = wmSensitiveMapper.selectList(null);
        //2.2获取所有的敏感词
        final List<String> strings = wmSensitives.stream().map(wmSensitive1 -> {
            String sensitives = wmSensitive1.getSensitives();
            return sensitives;
        }).collect(Collectors.toList());

        for (String string : strings) {
            if (string.equals(wmSensitive.getSensitives())){
                return true;
            }
        }
        return false;
    }

    /**
     * 查询敏感词
     * @param sensitiveDto
     * @return
     */
     @Override
    public ResponseResult findSensitive(SensitiveDto sensitiveDto) {
         //1.参数校验
         if (sensitiveDto == null){
             return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
         }

         //2.创建查询条件
         LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();
         //3.创建分页对象
         Page<WmSensitive> sensitivePage = new Page<>(sensitiveDto.getPage(), sensitiveDto.getSize());

         if (sensitiveDto.getName()!= null){
             queryWrapper.like(WmSensitive::getSensitives,sensitiveDto.getName());
         }
         Page<WmSensitive> wmSensitivePage = wmSensitiveMapper.selectPage(sensitivePage, queryWrapper);
         PageResponseResult result = new PageResponseResult(sensitiveDto.getPage(),sensitiveDto.getSize(), (int) wmSensitivePage.getTotal());
         result.setData(wmSensitivePage.getRecords());

         return result;
    }

    /**
     * 修改敏感词
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult updateSensitive(WmSensitive wmSensitive) {
        //1.参数校验
        if (wmSensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //2.判断是你否存在敏感词
        if (sensitiveExist(wmSensitive)){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }

        //保存敏感词
        wmSensitive.setCreatedTime(new Date());
        wmSensitiveMapper.insert(wmSensitive);
        return ResponseResult.okResult("敏感词修改成功！");
    }

    /**
     * 删除敏感词
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteSensitive(Integer id) {
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        wmSensitiveMapper.deleteById(id);
        return ResponseResult.okResult("敏感词删除成功！");
    }
}
